from django.contrib.auth.models import User
from django.db import models

class UserRank(models.Model):

    title = models.CharField(max_length=255)
    description = models.CharField(max_length=255)
    rank_icon = models.CharField(max_length=255)

    def __unicode__(self):
        return self.title

    class Meta:
        db_table = "user_ranks"


class UserProfile(models.Model):

    TYPE_INTERNAL = 0
    TYPE_VK = 1
    TYPE_VK_APPLICATION = 2

    TYPES = (
        (TYPE_INTERNAL, "Internal"),
        (TYPE_VK, "VK"),
        (TYPE_VK_APPLICATION, "VK Application")
    )

    user = models.ForeignKey(User, unique=True)
    avatar = models.CharField(max_length=255, null=True, blank=True)
    # not used yet
    balance = models.FloatField(default=0)
    rating = models.FloatField(default=0)
    #--
    icq = models.CharField(max_length=255, null=True, blank=True)
    skype = models.CharField(max_length=255, null=True, blank=True)
    #--
    type = models.IntegerField(choices=TYPES, default=TYPE_INTERNAL)
    vk_user_id = models.IntegerField(null=True, blank=True)
    ranks = models.ManyToManyField(UserRank)

    def get_user_name(self):
        if not "vk" in self.user.username:
          return self.user.username
        else:
          return "%s %s" % (self.user.first_name, self.user.last_name)

    def get_avatar(self):
        if "http:" in self.avatar:
            return self.avatar
        if self.avatar:
            return "/uploads/avatars/%s" % self.avatar
        else:
            return "/uploads/avatars/noavatar.png"

    def __str__(self):
        return self.user.username
        
    class Meta:
        db_table = "user_profiles"
